<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Request;

class WhiteListMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {

        $whitelist = config('maqu.API_ACCESS_WHITE_LIST');
        if($whitelist){
            $ips =explode(',',$whitelist);

            if(!in_array($request->getClientIp(),$ips)){
                return response()->json(array(
                    'status' => RESPONSE_FAILURE,
                    'code'=>'',
                    'message' => 'not allow to access!ip:' . $request->getClientIp(),
                    'auth_failure' => AUTH_FAILURE_NO,
                    'data' => []
                ), 200);
            }
        }

        return $next($request);
    }
}
